/**
 * Copyright HCP Technology 
 * www.hcp.vn
 */
package models;

import java.util.Date;
import java.util.List;

import siena.Id;
import siena.Index;
import siena.Model;
import siena.Query;
import util.Util;

public class UserModel extends Model{

	@Id
	public long id;
	@Index(value = "email_index")
	public String email;
	public String nickName;
	public String userId;
	public Date dateLogin;  	
	public boolean isAdmin;
	public boolean isActive;
	
	public UserModel(String email, String nickName, String userId,
			boolean isAdmin) {
		super();
		this.email = email;
		this.nickName = nickName;
		this.userId = userId;
		this.isAdmin = isAdmin;
		this.dateLogin = new Date();
		this.isActive = true;
	}
	
	static Query<UserModel> all(){
		return Model.all(UserModel.class);
	}
	
	public static UserModel getUserById(Long id){
		return all().getByKey(id);
	}
	
	public static UserModel getUserModelByEmail(String email){
		return all().filter("email", email).get();
	}
	
	public static void setAdminToUser(UserModel userModel){
		userModel.isAdmin = true;
		userModel.update();
	}
	
	public static void setDateLogin(UserModel userModel){
		userModel.dateLogin = new Date();
		userModel.update();
	}
	
	public static List<UserModel> getAll(){
		return all().fetch();
	}
	
	public static void setActiveUser(UserModel userModel){
		userModel.isActive = true;
		userModel.update();
	}
	
	public static void setDeactiveUser(UserModel userModel){
		userModel.isActive = false;
		userModel.update();
	}
	
	public static List<UserModel> findByLimit(int page, int limit){
		return all().fetch(limit, (page - 1) * Util.PAGE_ITEM);
	}
}
